package com.study.talk.app.group;

import android.content.Intent;
import android.os.Bundle;


import androidx.recyclerview.widget.LinearLayoutManager;

import com.study.talk.adapter.UserListCheckedListAdapter;
import com.study.talk.app.base.BaseActivity;
import com.study.talk.databinding.ActivityCreateGroupBinding;

import com.study.talk.entity.UserInfo;
import com.study.talk.entity.vo.SelectUserListVo;

import com.study.talk.util.GsonInstance;
import com.study.talk.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;


public class CreateGroupActivity extends BaseActivity {

    private ActivityCreateGroupBinding activityCreateGroupBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityCreateGroupBinding = ActivityCreateGroupBinding.inflate(getLayoutInflater());
        setContentView(activityCreateGroupBinding.getRoot());
        setSupportActionBar(activityCreateGroupBinding.createGroupToolbar);
        initView();
    }


    private void initView() {
        activityCreateGroupBinding.createGroupToolbar.setNavigationOnClickListener(v -> finish());
        List<UserInfo> friendList = getApp().friendList;
        UserListCheckedListAdapter adapter = new UserListCheckedListAdapter(friendList, this);
        activityCreateGroupBinding.startCreateGroupListView.setLayoutManager(new LinearLayoutManager(this));
        activityCreateGroupBinding.startCreateGroupListView.setAdapter(adapter);
        activityCreateGroupBinding.startCreateGroupBt.setOnClickListener(view -> {
            ArrayList<UserInfo> selectList = new ArrayList<>();
            for (UserInfo userInfo : friendList){
                if (Boolean.TRUE.equals(adapter.selectMap.get(userInfo.getId()))){
                    selectList.add(userInfo);
                }
            }
            ToastUtil.showToast(CreateGroupActivity.this,""+selectList.size());
            Intent intent = new Intent(CreateGroupActivity.this, CreateGroupInfoActivity.class);
            intent.putExtra("data", GsonInstance.instance.toJson(new SelectUserListVo(selectList)));
            startActivity(intent);
        });
    }
}